package com.hushao.lims.weblims.controller;
import com.hushao.lims.weblims.domain.ClassroomInfo;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.ClassroomService;
import com.hushao.lims.weblims.service.TeacherService;
import com.hushao.lims.weblims.utils.UserUtil;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author 930588706
 * @date 18/1/21
 * 教室
 */
@RequestMapping("/classroom")
@Controller
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 根据教室名称模糊查询教室列表（分页）
     * @param classroomName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "模糊查询实验室列表（分页）")
    @GetMapping
    public String listByClassroom(@RequestParam(value = "classroomName",required = false,defaultValue = "")String classroomName,
                                  @RequestParam(value = "async",required = false)boolean async,
                                  Pageable pageable,
                                  Model model)throws Exception{
        Page<ClassroomInfo> classroomInfoPage=classroomService.listClassroomByName(classroomName,pageable);
        List<ClassroomInfo> classrooms=classroomInfoPage.getContent();
        model.addAttribute("page",classroomInfoPage);
        model.addAttribute("classrooms",classrooms);
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());

        //获取数据库的角色而不是获取当前静态登陆角色
        TeacherInfo teacher1=teacherService.getById(teacher.getTeacherId());
        //是否是管理员或者超级管理员
        boolean isBo=userUtil.isAdministrator(teacher1)||userUtil.isAdmin(teacher1);
        model.addAttribute("isBo",isBo);
        return async==true?"classrooms/list ::#mainContainerRepleace":"classrooms/list";
    }


    /**
     * 获取添加实验室界面
     * @param model
     * @return
     */
    @ApiOperation(value = "获取添加实验室界面")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/add")
    public String addInterface(Model model){
        ClassroomInfo classroomInfo=new ClassroomInfo(null,null);
        model.addAttribute("classroom",classroomInfo);
        return "/classrooms/edit";
    }

    /**
     * 获取编辑班级界面
     * @param classroomId
     * @param model
     * @return
     */
    @ApiOperation(value = "获取编辑实验室界面")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/edit/{classroomId}")
    public String editInterface(@PathVariable("classroomId") Integer classroomId, Model model){
        ClassroomInfo classroomInfo=classroomService.getById(classroomId);
        model.addAttribute("classroom",classroomInfo);
        return "/classrooms/edit";
    }


    /**
     * 修改或增加实验室
     * @param classroom
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改或增加实验室操作服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @PostMapping
    public ResponseData saveOrUpdate(ClassroomInfo classroom, String teacherNumber)throws Exception{
        String message=classroomService.saveOrUpdate(classroom,teacherNumber);
        return new ResponseData(true,message);
    }

    /**
     * 删除实验室
     * @param classroomId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除实验室操作服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @DeleteMapping("/{classroomId}")
    public ResponseData delete(@PathVariable("classroomId") Integer classroomId)throws Exception {
        classroomService.remove(classroomId);
        return new ResponseData(true,"删除成功！");
    }

    /**
     * 实验室课程
     * @param classroomId
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取实验室课程")
    @GetMapping("/{classroomId}/timetables")
    public String timetables(@PathVariable("classroomId") Integer classroomId, Model model)throws Exception {
        ClassroomInfo classroom=classroomService.getById(classroomId);
        Map<String, Map<String, String>> map=classroomService.timetables(classroom);
        model.addAttribute("map",map);
        return "/classrooms/timetables";
    }

}
